<template>
  <!-- 检查事项 -->
  <div class="dialog-container">
    <el-dialog
      title="停产报告单"
      :visible="dialogVisible"
      @open="initData"
      @close="closeDialog"
    >
      <div class="table-tools-bar">
        <el-button type="primary" @click="handleAdd">新增</el-button>
      </div>
      <div class="table-panel">
        <el-table
          ref="table"
          class="grid-data-table"
          height="calc(100vh - 400px)"
          :data="tableData"
        >
          <el-table-column prop="id" label="id" width="40" />
          <el-table-column prop="startTime" label="停产开始日期" />
          <el-table-column prop="endTime" label="停产结束日期" />
          <el-table-column prop="remark" label="备注" />
          <el-table-column label="操作" width="220">
            <template slot-scope="scope">
              <el-button type="primary" size="small" @click="handleSee(scope.row)">查看</el-button>
              <el-button type="primary" size="small" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button type="primary" size="small" @click="handleDel(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-container">
          <Pagination
            v-show="query.total>0"
            :total="query.total"
            :page.sync="query.page"
            :limit.sync="query.limit"
            @pagination="initData"
          />
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" class="cancel-btn" @click="closeDialog">取消</el-button>
      </span>
    </el-dialog>
    <StopAddDialog
      :dialog-visible="stopAddDialogVisible"
      :dialog-type="dialogType"
      :add-update-obj="addUpdateObj"
      @closeDialog="stopAddDialogVisible = false"
      @flushPage="handleQuery"
    />
  </div>
</template>

<script>
import { getPage, del } from '@/api/base/stopProduction'
import Pagination from '@/components/Pagination'
import StopAddDialog from './StopAddDialog'

export default {
  name: 'StopDialog',
  components: {
    Pagination, StopAddDialog
  },
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    obj: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      query: {
        total: 0,
        page: 1,
        limit: 10,
        outletId: null
      },
      tableData: [],
      tableLoading: false,
      addUpdateObj: {},
      dialogType: 'detail',
      stopAddDialogVisible: false,
      rules: {
      // examineResult: [{ required: true, message: '请选择违法行为判定', trigger: 'change' }]
      }
    }
  },
  created() {
  },
  methods: {
    initData() {
      this.handleQuery()
    },
    handleQuery() {
      this.stopAddDialogVisible = false
      if (this.obj) {
        this.query.outletId = this.obj.id
        this.tableLoading = true
        const query = JSON.parse(JSON.stringify(this.query))
        getPage(query).then(res => {
          this.tableData = res.data.rows
          this.query.total = res.data.total
          this.tableLoading = false
        }).catch(() => {
          this.tableLoading = false
        })
      }
    },
    // 查看
    handleSee(row) {
      this.addUpdateObj = row
      this.dialogType = 'detail'
      this.stopAddDialogVisible = true
    },
    // 添加
    handleAdd() {
      this.addUpdateObj = {}
      this.addUpdateObj.outletId = this.obj.id
      this.stopAddDialogVisible = true
      this.dialogType = 'add'
    },
    // 修改
    handleUpdate(row) {
      this.addUpdateObj = row
      this.dialogType = 'update'
      this.stopAddDialogVisible = true
    },
    handleDel(row) {
      this.$confirm('确认删除此数据?', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del([row.id]).then(result => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.handleQuery()
        }).catch(() => {
        })
      }).catch(() => {
      })
    },
    closeDialog() {
      this.$emit('closeDialog')
      this.clearData()
      this.$refs['form'].clearValidate()
    },
    clearData() {
      this.query = {
        total: 0,
        page: 1,
        limit: 10,
        outletId: null
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
